import Api from './api'
import {getCookie, getServerBase} from '../util'


const api = new Api({
  baseURI: getServerBase(),
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

export default api

/**
* ajax获取数据
* type 请求类型 （get,post,put...)
* path 请求路径
* 请求参数 object
*/
export function AjaxServer(type, path, params, data) {
  if(!getCookie('token') || getCookie('token') == undefined || getCookie('token') == '' ){
    location.href = '/#/login'
  }
  params.token =getCookie('token')
  // data.data.backend_token = decodeURIComponent(getCookie('backend_token'));
  return api[type](path, { params, data: data})
}
/**
 * 下载文件
 * @param {*} type 
 * @param {*} path 
 * @param {*} params 
 * @param {*} data 
 */
export function Download(type, path, params, data){
  let str = ''
  for(let i in params){
    if(params[i]!=undefined){
      str+=i+'='+params[i]+'&'
    }
  }
  str+='token='+getCookie('token')
  window.open(getServerBase()+path+'?'+str)
}
